package com.luban.processor;

import com.luban.service.AService;
import com.luban.service.BService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cglib.proxy.Enhancer;

/**
 * Created by yinpeng on 2020/06/29
 */
public class TestAopBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(bean instanceof BService){
			System.out.println("这里可以执行cglib动态代理");
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(BService.class);
			enhancer.setCallback(new TestMethodInterceptor());
			enhancer.setCallbackType(TestMethodInterceptor.class);
			enhancer.setUseFactory(false);
			return enhancer.create();
		}else {
			return bean;
		}
	}
}
